<?php


namespace app\index\controller;

use think\Exception;

class Lockie extends Site
{
    /**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * ➡ 视频和图片调用访问
     **++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    /**
     * 图片防盗链
     * 调用方法：
     *      缩略图：域名/pimage/唯一UUID
     *        原图：域名/pimage/唯一UUID/1
     * @throws Exception
     * @throws \think\exception\PDOException
     */
     public function img_path(){
         header('Content-type:image/jpg');
         $uuid = $this->request->param("uuid");
         $yuantu = $this->request->param("yuan");
         //非法盗图
         $base64 = "";//非正规渠道引用该图
         //图片不存在
         $sls="";
         if($uuid){
             if(isset($_SERVER['HTTP_REFERER'])){
                 //判断$_SERVER['HTTP_REFERER']是不是以http://localhost/开始的
                 $var_arr=['http://www.demo.com/','http://demo.goovv.cc/'];
                 if(in_array($_SERVER['HTTP_REFERER'],$var_arr)){
                     //查询地址
                     $data= $this->get_tab_info(" where uuid='".$uuid."'");
                     if(isset($data[0]) && $data[0]){
                         if($yuantu && intval($yuantu)===1){
                             $sls=base64EncodeImage(ROOT_PATH.'public'.$data[0]['file_path'].$data[0]['file_name']);
                         }else{
                             $sls=base64EncodeImage(ROOT_PATH.'public'.$data[0]['file_path'].$data[0]['file_name_sn']);
                         }
                     }
                     echo file_get_contents($sls);//'<img src="'.$sls.'"/>';
                     die;
                 }
             }
         }
         echo file_get_contents($base64);
     }
    /**
     * 视频防盗链
     * 调用方法：
     *      播放：域名/pvideo/唯一UUID
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function video_path(){
        $uuid = $this->request->param("uuid");
        //非法盗图
        $base64 = "";//非正规渠道引用该图
        //图片不存在
        $sls="";
        if($uuid){
            if(isset($_SERVER['HTTP_REFERER'])){
                //判断$_SERVER['HTTP_REFERER']是不是以http://localhost/开始的
                $var_arr=['http://www.demo.com/'];
                if(in_array($_SERVER['HTTP_REFERER'],$var_arr)){
                    //查询地址
                    $data= $this->get_tab_info(" where uuid='".$uuid."'");
                    if(isset($data[0]) && $data[0]){
                        $sls=ROOT_PATH.'public'.$data[0]['file_path'].$data[0]['file_name'];
                        $sls = str_replace('/','\\',$sls);
                    }
                   $this->PutMovie($sls);$data=null;
                }
                $var_arr=null;
            }
        }
        echo file_get_contents($base64);
        $uuid = null;$base64=null;$sls=null;
    }

    /**
     * 文件地址
     * 调用方法：
     *      下载：域名/pfile/唯一UUID
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function file_path(){
        $uuid = $this->request->param("uuid");
        if($uuid){
            //查询地址
            $data= $this->get_tab_info(" where uuid='".$uuid."'");
            if(isset($data[0]) && $data[0]){
                redirect(get_current_url_ym().$data[0]['file_path'].$data[0]['file_name']);
                die;
            }
        }
        $this->error("文件地址错误，文件不存在！");
    }
    /**
     * 输出视频
     * @param $file
     */
    private function PutMovie($file) {
        ini_set('memory_limit','512M');
        header("Content-type: video/mp4");
        header("Accept-Ranges: bytes");

        ob_start(); // ------ 开启缓冲区
        $size = filesize($file);

        if(isset($_SERVER['HTTP_RANGE'])){
            header("HTTP/1.1 206 Partial Content");
            list($name, $range) = explode("=", $_SERVER['HTTP_RANGE']);
            list($begin, $end) =explode("-", $range);
            if($end == 0) $end = $size - 1;
        }
        else {
            $begin = 0; $end = $size - 1;
        }

        header("Content-Length: " . ($end - $begin + 1));
        header("Content-Disposition: filename=".basename($file));
        header("Content-Range: bytes ".$begin."-".$end."/".$size);

        try {
            $fp = fopen($file, 'r');
        } catch (\Exception $e) {
            echo $e->getTraceAsString();exit;
        }
        fseek($fp, $begin);
        $contents = '';

        while(!feof($fp)) {
            $p = min(1024, $end - $begin + 1);
            //$begin += $p;
            $contents .= fread($fp, $p);
            //echo fread($fp, $p);
        }
        //$contents = ltrim($contents, "\XEF\XBB\XBF");
        ob_end_clean();            // ------ 清除缓冲区
        ob_clean();
        //$contents = substr($contents, 3);
        fclose($fp);

        exit($contents);
    }

}